* Table 2: Winners, losers, and the number of assigned applicants
*

clear all
use contract_id_orig fulltime_orig funding_orig rank_of_admission_orig program_id_orig ///
	contract_id_h2 fulltime_h2 funding_h2 winner_h2 rank_of_admission_h2 program_id_h2 ///
	contract_id_h4 fulltime_h4 funding_h4 winner_h4 rank_of_admission_h4 program_id_h4 ///
	loser_h2 loser_h4 winner_h2 winner_h4 using "${data_matchings}", clear

cap file close sumstat
file open sumstat using "${table2}", write replace
file write sumstat "\begin{tabular}{l S[table-number-alignment = center, table-format = 5.0] S[table-number-alignment = center, table-format = 5.0] S[table-number-alignment = center, table-format = 5.0]} \\ \hline\hline" _n
file write sumstat " & \multicolumn{1}{c}{Benchmark} & & \multicolumn{1}{c}{Preference flip } \\ " _n
file write sumstat " & \multicolumn{1}{c}{SR-DA} & \multicolumn{1}{c}{SP-DA} & \multicolumn{1}{c}{algorithm} \\ " _n
file write sumstat " & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)}  \\ \hline" _n
file write sumstat " \multicolumn{4}{l}{A. {\it Number of assigned applicants}} \\ " _n
	* Number of students assigned ----------------------------------------------
	count if contract_id_orig != .
	local benchmark_count: display %12.0fc r(N)
	
	forvalues i = 2(2)4 {
		count if contract_id_h`i' != .
		local h`i'_count: display %12.0fc r(N)
	}
	
	file write sumstat "Assigned to a contract & `benchmark_count' & `h4_count' & `h2_count' \\ " _n

	* Assigned to a state-funded contract ---------------------------------------
	count if contract_id_orig != . & funding_orig == 1
	local benchmark_count: display %12.0fc r(N)
	
	forvalues i = 2(2)4 {
		count if contract_id_h`i' != . & funding_h`i' == 1
		local h`i'_count: display %12.0fc r(N)
	}
	
	file write sumstat "Assigned to a state-funded contract & `benchmark_count' & `h4_count' & `h2_count' \\ " _n
	
	* Assigned to self-funded contract -----------------------------------------
	count if contract_id_orig != . & funding_orig == 2
	local benchmark_count: display %12.0fc r(N)
	
	forvalues i = 2(2)4 {
		count if contract_id_h`i' != . & funding_h`i' == 2
		local h`i'_count: display %12.0fc r(N)
	}
	
	file write sumstat "Assigned to a self-funded contract & `benchmark_count' & `h4_count' & `h2_count' \\ " _n
	
	* Assigned to self-funded w/ full time contract ----------------------------
	count if contract_id_orig != . & funding_orig == 2 & fulltime_orig == 1
	local benchmark_count: display %12.0fc r(N)
	
	forvalues i = 2(2)4 {
		count if contract_id_h`i' != . & funding_h`i' == 2 & fulltime_h`i' == 1
		local h`i'_count: display %12.0fc r(N)
	}
	
	file write sumstat "Assigned to a self-funded, full-time contract & `benchmark_count' & `h4_count' & `h2_count' \\ " _n
	
	
	* Winners ------------------------------------------------------------------
	file write sumstat "\multicolumn{4}{l}{}\\ " _n
	
	forvalues i = 2(2)4 {
		count if winner_h`i' == 1
		local w`i':  display %12.0fc r(N)
	
	}
	file write sumstat "B. {\it Winners} & & `w4'& `w2' \\ " _n
	
	forvalues i = 2(2)4 {
		count if winner_h`i' == 1 & rank_of_admission_orig == .
		local w`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- Newly assigned &  & `w4' & `w2' \\ " _n
	
	forvalues i = 2(2)4 {
		count if winner_h`i' == 1 & rank_of_admission_orig != . & program_id_h`i' != program_id_orig
		local w`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- New program & & `w4' & `w2' \\ " _n
	
		forvalues i = 2(2)4 {
		count if winner_h`i' == 1 & rank_of_admission_orig != . & program_id_h`i' == program_id_orig
		local w`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- Same program, preferred financial terms & & `w4' & `w2' \\ " _n
			
	* Losers ------------------------------------------------------------------
	file write sumstat "\multicolumn{4}{l}{}\\ " _n
	
	forvalues i = 2(2)4 {
		count if loser_h`i' == 1
		local l`i':  display %12.0fc r(N)
	
	}
	file write sumstat "C. {\it Losers} &  & `l4' & `l2' \\ " _n
	
	forvalues i = 2(2)4 {
		count if loser_h`i' == 1 & rank_of_admission_h`i' == .
		local l`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- Newly unassigned & & `l4' & `l2' \\ " _n
	
	forvalues i = 2(2)4 {
		count if loser_h`i' == 1 & rank_of_admission_h`i' != . & program_id_h`i' != program_id_orig
		local l`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- New program & & `l4' & `l2' \\ " _n
	
		forvalues i = 2(2)4 {
		count if loser_h`i' == 1 & rank_of_admission_h`i' != . & program_id_h`i' == program_id_orig
		local l`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- Same program, less preferred financial terms &   & `l4' & `l2' \\ \hline\hline" _n
	file write sumstat "\multicolumn{4}{p{14cm}}{{\it Notes}: The table presents the number of assigned applicants under various stable assignments (Panel A). Panels B and C describe the number of winners and losers from changing the benchmark \SRDA to \SPDA and to our alternate algorithms. Column (1) shows the benchmark assignment. Column (2) presents the \SPDA assignment. Column (3) presents the preference flip algorithm assignment.} \\  " _n
file write sumstat "\end{tabular}" _n
file close sumstat
